package com.yan.control;

import com.yan.pojo.Student;
import com.yan.pojo.Teacher;
import com.yan.service.AdminService;
import com.yan.service.StudentService;
import com.yan.service.TeacherService;
import com.yan.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;



    @RequestMapping("/getTeacherByName")
    @ResponseBody
    public Msg getTeacherByName(@RequestParam("name") String teacherName){
        Teacher teacherByName = teacherService.getTeacherByName(teacherName);
        if(teacherByName != null){
            return Msg.success();
        }else {
            return Msg.fail();
        }
    }

    @RequestMapping("/userRegister")
    @ResponseBody
    public Msg userRegister(@RequestParam("username")String username, @RequestParam("password")String password,@RequestParam("type") String type){

        if(Integer.parseInt(type) == 1){
            boolean b = adminService.addTeacher(new Teacher(null, username, password));
            return b ? Msg.success() : Msg.fail();
        }else if(Integer.parseInt(type) == 2){
            boolean b = studentService.registerStudent(Integer.parseInt(username), password);
            return b ? Msg.success() : Msg.fail();
        }
        return Msg.fail();
    }

    @RequestMapping("/getStudentById")
    @ResponseBody
    public Msg getStudentById(@RequestParam("studentId")String studentId){
        Student student = adminService.getStudentById(Integer.parseInt(studentId));
        System.out.println(student);
        System.out.println(studentId);
        if(student == null){ //这个解决了
            return Msg.fail();
        }else{
            return Msg.success().add("data", student);
        }
    }

    @RequestMapping("/getStudentList")
    @ResponseBody
    public Msg getStudentList(){
        List<Student> list = adminService.studentList();
        return Msg.success().add("studentList",list);
    }

    @RequestMapping("/saveEditStudent")
    @ResponseBody
    public Msg saveEditStudent(@Valid Student student){
        boolean b = adminService.updateStudent00(student);
        return  b ? Msg.success() : Msg.fail();
    }

    @RequestMapping("/saveEditTeacher")
    @ResponseBody
    public Msg saveEditTeacher(@Valid Teacher teacher){
        boolean b = adminService.saveEditTeacher(teacher);
        return  b ? Msg.success() : Msg.fail();
    }

    @RequestMapping("/addStudent")
    @ResponseBody
    public Msg addStudent(@Valid Student student){
        boolean b = adminService.addStudent(student);
        return  b ? Msg.success() : Msg.fail();
    }

    @RequestMapping("/delStudentById")
    @ResponseBody
    public Msg delStudentById(@RequestParam("studentId")String studentId){
        boolean b = adminService.delStudentByStudentId(Integer.parseInt(studentId));
        return  b ? Msg.success() : Msg.fail();
    }

    @RequestMapping("/delStudentByIds")
    @ResponseBody
    public Msg delStudentByIds(@RequestParam("ids")String ids)
    {
        String[] split = ids.split("-");
        for (int i = 0; i < split.length; i++) {
            adminService.delStudentByStudentId(Integer.parseInt(split[i]));
        }
        return Msg.success();
    }

    @RequestMapping("/findStudent")
    @ResponseBody
    public Msg findStudent(@RequestParam("keyWords")String keyWords){
        List<Student> student = adminService.findStudent(keyWords);
        return Msg.success().add("list",student);
    }

    @RequestMapping("/getStudentsBeDeleted")
    @ResponseBody
    public Msg getStudentsBeDeleted(){
        List<Student> list = adminService.getStudentsBeDeleted();
        return Msg.success().add("list", list);
    }

    @RequestMapping("/clearAllDeleted")
    @ResponseBody
    public Msg clearAllDeleted(){
        boolean b = adminService.clearAllDelete();
        return Msg.success();
    }

    @RequestMapping("/getTeacherList")
    @ResponseBody
    public Msg getTeacherList(){
        List<Teacher> list = teacherService.getTeacherList();
        return Msg.success().add("list", list);
    }

    @RequestMapping("/getTeacherById")
    @ResponseBody
    public Msg getTeacherById(@RequestParam("id")String id){
        Teacher teacher = teacherService.getTeacherById(Integer.parseInt(id));
        return Msg.success().add("data", teacher);
    }

    @RequestMapping("/editTeacherInfo")
    @ResponseBody
    public Msg editTeacherInfo(@Valid Teacher teacher){
        boolean b = teacherService.updateTeacherPassword(teacher);
        return b ? Msg.success() : Msg.fail();
    }

    @RequestMapping("/addTeacher")
    @ResponseBody
    public Msg addTeacher(@Valid Teacher teacher){
        boolean b = adminService.addTeacher(teacher);
        return b ? Msg.success() : Msg.fail();
    }

    @RequestMapping("/delTeacherById")
    @ResponseBody
    public Msg delTeacherById(@RequestParam("id")String id){
        boolean b = adminService.delTeacherById(Integer.parseInt(id));
        return b ? Msg.success() : Msg.fail();
    }

    @RequestMapping("/delTeacherByIds")
    @ResponseBody
    public Msg delTeacherByIds(@RequestParam("ids")String ids){
        String[] split = ids.split("-");
        for (int i = 0; i < split.length; i++) {
            adminService.delTeacherById(Integer.parseInt(split[i]));
        }
        return Msg.success();
    }









}
